package com.hnkywl.jip.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.*;

import javax.xml.parsers.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Component
public class XmlParse {
    private InterfaceParam interfaceParam;
    //Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
    //内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
    private Document doc = null;
    public static final String VCHRID = "vchrid";
    public static final String VCHRTYPE = "vchrtype";
    public static final String VCHRNUM = "vchrnum";
    public static final String RESPONSEFLAG = "responseflag";
    public static final String RESPONSEMESSAGE = "responsemessage";

    public void init(String xml) throws Exception {
        //很明显该类是一个单例,先获取产生DocumentBuilder工厂
        //的工厂,在通过这个工厂产生一个DocumentBuilder,
        //DocumentBuilder就是用来产生Document的
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        //这个Document就是一个XML文件在内存中的镜像
        ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
        doc = db.parse(in);
    }

    public Map<String, String> getContent(String xml) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            if (xml != null) {
                String regex = "encoding=[\"\'][\\w-]*[\"']";
                if ("GBK".equals(interfaceParam.getXmlEncoding())) {
                    xml = xml.replaceFirst(regex, "encoding=\"GBK\"");
                    this.init(ConvertCharSet.toGBK(xml));
                }
                if ("UTF-8".equals(interfaceParam.getXmlEncoding())) {
                    xml = xml.replaceFirst(regex, "encoding=\"UTF-8\"");
                    this.init(ConvertCharSet.toUTF8(xml));
                }
            } else {
                result.put(XmlParse.RESPONSEFLAG, "0");
                result.put(XmlParse.RESPONSEMESSAGE, "久其财务接口无法访问，请稍后重试，或联系久其服务人员!");
                return result;
            }
            String flag = getTextContent(XmlParse.RESPONSEFLAG);
            String message = getTextContent(XmlParse.RESPONSEMESSAGE);
            if (flag != null && "0".equals(flag)) {
                result.put(XmlParse.RESPONSEFLAG, flag);
                result.put(XmlParse.RESPONSEMESSAGE, message);
            } else {
                result.put(XmlParse.RESPONSEFLAG, flag);
                result.put(XmlParse.RESPONSEMESSAGE, message);
                String vchrid = getTextContent(XmlParse.VCHRID);
                result.put(XmlParse.VCHRID, vchrid);
                String vchrnum = getTextContent(XmlParse.VCHRNUM);
                result.put(XmlParse.VCHRNUM, vchrnum);
                String vchrtype = getTextContent(XmlParse.VCHRTYPE);
                result.put(XmlParse.VCHRTYPE, vchrtype);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, "返回的xml数据无法解析!" + e.getMessage());
        }
        return result;
    }

    public String getTextContent(String tag) {
        String temp = null;
        NodeList nodeList = doc.getElementsByTagName(tag);
        if (nodeList != null && nodeList.getLength() != 0) {
            Node responseFlag = nodeList.item(0);
            temp = responseFlag.getTextContent();
        }
        return temp;
    }

//    public static void main(String[] args) throws Exception {
//        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
//                "<response> " +
//                "  <id>1a3c7797ec214532907a7551176608a8</id> " +
//                "  <responseflag>1</responseflag> " +
//                "  <responsemessage>生成凭证成功</responsemessage> " +
//                "  <voucherinfo> " +
//                "   <vchrid>3D3EADE220000001651478829E373B26</vchrid> " +
//                "   <vchrnum>39</vchrnum> " +
//                "   <vchrtype>银行</vchrtype> " +
//                "  </voucherinfo> " +
//                " </response>";
//        XmlParse parse = new XmlParse();
//        InterfaceParam p = new InterfaceParam();
//        p.setXmlEncoding("UTF-8");
//        parse.setInterfaceParam(p);
//        //我的XML文件
//        Map m = parse.getContent(xml);
//        System.out.print("");
//    }

    @Autowired
    public void setInterfaceParam(InterfaceParam interfaceParam) {
        this.interfaceParam = interfaceParam;
    }
}
